package com.laikan.reader.core.utils;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/6/24.
 */
public class CountDownTimerUtils extends CountDownTimer {

    private TextView mTextView;
    private SpannableString spannableString;
    ForegroundColorSpan span,span1;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false);
        mTextView.setText(millisUntilFinished / 1000 + " 秒后 重新发送");
        spannableString = new SpannableString(mTextView.getText().toString());
        span = new ForegroundColorSpan(Color.RED);
        span1 = new ForegroundColorSpan(Color.parseColor("#999999"));
        spannableString.setSpan(span,0,2,SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(span1,2,spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
    }

    @Override
    public void onFinish() {
      mTextView.setClickable(true);
      mTextView.setText("获取验证码");
      mTextView.setTextColor(Color.parseColor("#66ccdd"));
    }
}
